package com.gemalto.Kryptik.Gui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import com.gemalto.Kryptik.Util.*;
import com.gemalto.Kryptik.Algo.*;
import com.gemalto.Kryptik.Gui.*;
import com.gemalto.Kryptik.*;

public class NewGui extends JFrame {


	private SearchableTextArea plainText, encryptedText ;
	private CountedTextField signText;
	private JPanel mainPanel, buttonPanel, statusPanel, keyPanel, macPanel;
	private	static MessageArea messageArea;
	private static Counter counter;
	private Indicator algoIndicator, macAlgoIndicator;
	private JButton encryptButton, decryptButton, signButton;
	private DetachableTabbedPane tabbedPane; 
	private MainTab mainTab;
	private static IniFiles ini;

	private AlgoManager algoManager, macAlgoManager;

	public NewGui(AlgoManager algoManager, AlgoManager macAlgoManager){

		ini = Util.getIni();
		setTitle("Kryptik");
		this.algoManager=algoManager;
		this.macAlgoManager=macAlgoManager;

		String text=null;
		ImageIcon icon;
		ImageIcon icon1 = Util.getIcon("class.gif");
		ImageIcon icon2 = Util.getIcon("compile.gif");
		ImageIcon icon3 = Util.getIcon("info.gif");
		ImageIcon icon4 = Util.getIcon("editor.gif");
		ImageIcon icon5 = Util.getIcon("BSH.gif");

	        mainPanel = new JPanel();
        	mainPanel.setLayout(new javax.swing.BoxLayout(mainPanel, 1));
        	mainPanel.setBorder(new javax.swing.border.EmptyBorder(new java.awt.Insets(1, 1, 1, 1)));
//		mainPanel.setFont(new java.awt.Font ("Courier New", 0, 8));


		tabbedPane = new DetachableTabbedPane();

/*	        mainTab = new JPanel();
        	mainTab.setLayout(new javax.swing.BoxLayout(mainTab, 1));
        	mainTab.setBorder(new javax.swing.border.EmptyBorder(new java.awt.Insets(1, 1, 0, 0)));
*/
		statusPanel = new JPanel();
        	statusPanel.setLayout(new javax.swing.BoxLayout(statusPanel, 0));
        	statusPanel.setBorder(new javax.swing.border.EmptyBorder(new java.awt.Insets(2, 0, 0, 0)));
		Beanshell.setVariable("statusPanel", statusPanel);
	
		messageArea = new MessageArea(Util.getTranslation("COPYRIGHT"));
		algoIndicator = new Indicator(algoManager);
		macAlgoIndicator = new Indicator(macAlgoManager);
		counter = new Counter();


		mainTab=new MainTab(algoManager, macAlgoManager);
		                  	
		keyPanel = new GenericAlgoTab(algoManager);
		macPanel = new GenericAlgoTab(macAlgoManager);
		


		//getContentPane().add(mainTab);
		getContentPane().add(mainPanel);
		mainPanel.add(tabbedPane);

//		if (ini.getBoolValue("GUI","ICON_IN_TABS")) icon = icon1; else icon = null;
//		if (ini.getBoolValue("GUI","TEXT_IN_TABS")) text = Kryptik.getTranslation("MAIN_TAB"); else text=null;
//		tabbedPane.addTab(text, icon, mainTab);
		addTab("MAIN_TAB",icon1, mainTab);

//		if (ini.getBoolValue("GUI","ICON_IN_TABS")) icon = icon2; else icon = null;//
//		if (ini.getBoolValue("GUI","TEXT_IN_TABS")) text = Kryptik.getTranslation("ALGO_TAB"); else text=null;
//		tabbedPane.addTab(text, icon, keyPanel);
		addTab("ALGO_TAB",icon2, keyPanel);

//		if (ini.getBoolValue("GUI","ICON_IN_TABS")) icon = icon4; else icon = null;
//		if (ini.getBoolValue("GUI","TEXT_IN_TABS")) text = Kryptik.getTranslation("MACALGO_TAB"); else text=null;
//		tabbedPane.addTab(text, icon, macPanel);
		addTab("MACALGO_TAB",icon4, macPanel);

//		if (ini.getBoolValue("GUI","ICON_IN_TABS")) icon = icon3; else icon = null;
//		if (ini.getBoolValue("GUI","TEXT_IN_TABS")) text = Kryptik.getTranslation("ABOUT_TAB"); else text=null;
//		tabbedPane.addTab(text, icon, new AboutTab(ini));

		Beanshell.setVariable("messageArea", messageArea);
		Beanshell.setVariable("counter",counter);
//		Beanshell.setVariable("algoManager", algoManager);
//		Beanshell.setVariable("macAlgoManager", macAlgoManager);
/*		Beanshell.setVariable("plainText", mainTab.getPlainText());
		Beanshell.setVariable("encryptedText", mainTab.getEncryptedText());
		Beanshell.setVariable("signText", mainTab.getSignText());
		Beanshell.setVariable("encryptButton", encryptButton);
		Beanshell.setVariable("decryptButton", decryptButton);
		Beanshell.setVariable("signButton", signButton);
*/		Beanshell.setVariable("gui",this);


		ConsoleTab ctab=new ConsoleTab("BSH Console");
/*		ctab.setVariable("messageArea",messageArea); 
		ctab.setVariable("counter",counter);
		ctab.setVariable("algoManager", algoManager);
		ctab.setVariable("macAlgoManager", macAlgoManager);
		ctab.setVariable("plainText", mainTab.getPlainText());
		ctab.setVariable("encryptedText", mainTab.getEncryptedText());
		ctab.setVariable("signText", mainTab.getSignText());
		ctab.setVariable("encryptButton", encryptButton);
		ctab.setVariable("decryptButton", decryptButton);
		ctab.setVariable("signButton", signButton);
		ctab.setVariable("gui",this);
*/
		addTab("CONSOLE_TAB", icon5, ctab);
		addTab("ABOUT_TAB",icon3, new AboutTab());



/*
		tabbedPane.addTab(Kryptik.getTranslation("ALGO_TAB"),icon2,keyPanel);
		tabbedPane.addTab(Kryptik.getTranslation("MACALGO_TAB"),icon4,macPanel);
		tabbedPane.addTab(Kryptik.getTranslation("ABOUT_TAB"),icon3, new AboutTab(ini));
*/

		mainPanel.add(statusPanel);
		statusPanel.add(messageArea);
		statusPanel.add(algoIndicator);
		statusPanel.add(macAlgoIndicator);
		statusPanel.add(counter);

                try {
                        Thread.sleep(300);
                } catch ( InterruptedException e){
                }
		pack();

		/**
		 * Set width and height of the window, which was used last time and is saved in
		 * INI file.
		 */
		setLocation(ini.getIntValue("GUI","X"), ini.getIntValue("GUI","Y"));
		setSize(ini.getIntValue("GUI","W"),ini.getIntValue("GUI","H"));


		/**
		 * Do not allow to resize main window bellow WINDOW_MIN_WIDTH x WINDOW_MIN_HEIGHT
		 */
        addComponentListener(new ComponentAdapter() { 
			private int minWidth =ini.getIntValue("GUI","WINDOW_MIN_WIDTH");
			private int minHeight=ini.getIntValue("GUI","WINDOW_MIN_HEIGHT");

			public void componentResized(ComponentEvent event) { 
				if ( minWidth < 0) minWidth=300;
				if ( minHeight < 0) minHeight=200;
				// Check if the event concerns a JFrame 
				if(event.getSource() instanceof JFrame) { 
					// The JFrame is beeing resized, get the new size 
					int width = getWidth(); 
					int height = getHeight(); 
					if(width < minWidth) 
						width = minWidth; 
					if(height < minHeight) 
						height = minHeight; 
					// The JFrame should not be smaller than 300x300 
					setSize(width, height); 
				} 
			} 
		}); 

        	addWindowListener(new WindowAdapter() {
            		public void windowClosing(WindowEvent evt) {
					Util.saveIni((JFrame)evt.getSource());
				setVisible(false);
				System.exit(0);
            		}});

		//show();
	}


	public void addTab(String tabName, ImageIcon icon, JPanel tab){
		String text;
		if (!ini.getBoolValue("GUI","ICON_IN_TABS")) icon = null;
		if (ini.getBoolValue("GUI","TEXT_IN_TABS")) {
			if ((text=Util.getTranslation(tabName)).equals("")) text=tabName; 
		} else text=null;
		
		tabbedPane.addTab(text, icon, tab);
	}



	public void removeTab(int i){
		tabbedPane.remove(i);
	}


	public JPanel getTab(int i){
		if (i<tabbedPane.getTabCount())
			return (JPanel)tabbedPane.getComponentAt(i);
		else
			return null;
	}	

	public void setAlgoManager(AlgoManager a){
		algoManager=a;
	}



	public static Counter getCounter(){
		return counter;
	}	


	public static MessageArea getMessageArea(){
		return messageArea;
	}

/*

*/
}
